<template>
  <div class="edit-page-wrap">
    <el-tabs
      v-model="data.activeTab"
      class="demo-tabs"
      @tab-click="handleClickTab"
    >
      <el-tab-pane label="短信通道配置" name="sms">
        <Head title="以下配置参数请从您购买的短信通道平台获取">
          <template #content>
            <el-form
              ref="ruleFormSmsRef"
              :model="ruleForm"
              :rules="rules"
              label-width="130px"
              class="edit-form-wrap"
            >
              <el-form-item label="accessKeyId" prop="accesskeyId">
                <el-input
                  v-model.trim="ruleForm.accesskeyId"
                  placeholder="请输入accessKeyId"
                  maxlength="30"
                  clearable
                />
              </el-form-item>
              <el-form-item label="accessKeySecret" prop="accesskeySecret">
                <el-input
                  v-model.trim="ruleForm.accesskeySecret"
                  placeholder="请输入accessKeySecret"
                  maxlength="30"
                  clearable
                />
              </el-form-item>
              <el-form-item label="短信签名" prop="signName">
                <el-input
                  v-model.trim="ruleForm.signName"
                  placeholder="请输入短信签名"
                  maxlength="30"
                  clearable
                />
              </el-form-item>
            </el-form>
          </template>
        </Head>
        <div class="edit-btn-wrap">
          <el-button type="primary" plain @click="back">取消</el-button>
          <el-button type="primary" v-btnAntiShake="onSubmit">保存</el-button>
        </div>
      </el-tab-pane>
      <el-tab-pane label="支付配置" name="wx">
        <Head title="以下配置参数请从微信商户号、微信开放平台获取">
          <template #content>
            <el-form
              ref="ruleFormWxRef"
              :model="ruleForm"
              :rules="rules"
              label-width="130px"
              class="edit-form-wrap"
            >
              <el-form-item
                v-if="ruleForm.platfromType === 1"
                label="微信商户号"
                prop="wechatAccount"
              >
                <el-input
                  v-model.trim="ruleForm.wechatAccount"
                  placeholder="请输入微信商户号"
                  maxlength="30"
                  clearable
                />
              </el-form-item>
              <el-form-item label="AppID" prop="appId">
                <el-input
                  v-model.trim="ruleForm.appId"
                  placeholder="请输入AppID"
                  maxlength="30"
                  clearable
                />
              </el-form-item>
              <el-form-item label="AppSecret" prop="appSecret">
                <el-input
                  v-model.trim="ruleForm.appSecret"
                  placeholder="请输入AppSecret"
                  maxlength="50"
                  clearable
                />
              </el-form-item>
              <el-form-item
                v-if="ruleForm.platfromType === 1"
                label="商户私钥"
                prop="merchantSecret"
              >
                <el-input
                  v-model.trim="ruleForm.merchantSecret"
                  placeholder="请输入商户私钥"
                  maxlength="2000"
                  clearable
                />
              </el-form-item>
              <el-form-item
                v-if="ruleForm.platfromType === 1"
                label="APIv3秘钥"
                prop="aplvSecret"
              >
                <el-input
                  v-model.trim="ruleForm.aplvSecret"
                  placeholder="请输入APIv3秘钥"
                  maxlength="50"
                  clearable
                />
              </el-form-item>
              <el-form-item
                v-if="ruleForm.platfromType === 1"
                label="证书序列号"
                prop="serialNum"
              >
                <el-input
                  v-model.trim="ruleForm.serialNum"
                  placeholder="请输入证书序列号"
                  maxlength="50"
                  clearable
                />
              </el-form-item>
            </el-form>
          </template>
        </Head>
        <Head
          title="以下配置参数由对接的支付平台提供"
          v-if="ruleForm.platfromType !== 1"
        >
          <template #content>
            <el-form
              ref="ruleFormYlRef"
              :model="ruleForm"
              :rules="rules"
              label-width="130px"
              class="edit-form-wrap"
            >
              <el-form-item label="银联AppId" prop="umsAppId">
                <el-input
                  v-model.trim="ruleForm.umsAppId"
                  placeholder="请输入银联AppId"
                  maxlength="50"
                  clearable
                />
              </el-form-item>
              <el-form-item label="银联AppKey" prop="umsAppKey">
                <el-input
                  v-model.trim="ruleForm.umsAppKey"
                  placeholder="请输入银联AppKey"
                  maxlength="50"
                  clearable
                />
              </el-form-item>
              <el-form-item label="银联商户号Mid" prop="umsMerchant">
                <el-input
                  v-model.trim="ruleForm.umsMerchant"
                  placeholder="请输入银联商户号Mid"
                  maxlength="30"
                  clearable
                />
              </el-form-item>
              <el-form-item label="银联终端号tid" prop="umsTerminal">
                <el-input
                  v-model.trim="ruleForm.umsTerminal"
                  placeholder="请输入银联终端号tid"
                  maxlength="30"
                  clearable
                />
              </el-form-item>
              <el-form-item label="银联通讯密钥" prop="umsCommunicate">
                <el-input
                  v-model.trim="ruleForm.umsCommunicate"
                  placeholder="请输入银联通讯密钥"
                  maxlength="100"
                  clearable
                />
              </el-form-item>
              <el-form-item label="银联系统编号" prop="umsSystem">
                <el-input
                  v-model.trim="ruleForm.umsSystem"
                  placeholder="请输入银联系统编号"
                  maxlength="30"
                  clearable
                />
              </el-form-item>
            </el-form>
          </template>
        </Head>
        <div class="edit-btn-wrap">
          <el-button type="primary" plain @click="back">取消</el-button>
          <el-button type="primary" v-btnAntiShake="onSubmit">保存</el-button>
        </div>
      </el-tab-pane>
      <el-tab-pane label="互联互通配置" name="1">
        <el-form
          ref="ruleFormIotRef"
          :model="ruleForm"
          :rules="rules"
          label-width="130px"
          class="edit-form-wrap"
        >
          <el-form-item label="配置开关" prop="type">
            <el-switch
              v-model="ruleForm.type"
              inactive-color="#d4dbf5"
            ></el-switch>
            <span style="margin-left: 10px" v-if="ruleForm.type">开启</span>
            <span style="margin-left: 10px" v-else>关闭</span>
          </el-form-item>
          <el-form-item
            label="企业密钥"
            prop="enterpriseKey"
            v-if="ruleForm.type"
          >
            <el-input
              v-model.trim="ruleForm.enterpriseKey"
              placeholder="请输入号企业密钥"
              maxlength="50"
              clearable
            />
          </el-form-item>
          <el-form-item label="消息密钥" prop="messageKey" v-if="ruleForm.type">
            <el-input
              v-model.trim="ruleForm.messageKey"
              placeholder="请输入消息密钥"
              maxlength="50"
              clearable
            />
          </el-form-item>
          <el-form-item label="初始化向量" prop="initial" v-if="ruleForm.type">
            <el-input
              v-model.trim="ruleForm.initial"
              placeholder="请输入初始化向量"
              maxlength="50"
              clearable
            />
          </el-form-item>
          <el-form-item
            label="签名密钥"
            prop="signatureKey"
            v-if="ruleForm.type"
          >
            <el-input
              v-model.trim="ruleForm.signatureKey"
              placeholder="请输入签名密钥"
              maxlength="50"
              clearable
            />
          </el-form-item>
          <el-form-item
            label="互联互通方接口url"
            prop="trafficUrl"
            v-if="ruleForm.type"
          >
            <el-input
              v-model.trim="ruleForm.trafficUrl"
              placeholder="请输入互联互通方接口url"
              maxlength="100"
              clearable
              disabled
            />
          </el-form-item>
        </el-form>
        <div class="edit-btn-wrap">
          <el-button type="primary" plain @click="back">取消</el-button>
          <el-button type="primary" v-btnAntiShake="onSubmit">保存</el-button>
        </div>
      </el-tab-pane>
      <el-tab-pane label="电子发票配置（诺诺）" name="2" v-if="lznt">
        <el-form
          ref="ruleFormInvoiceRef"
          :model="ruleForm1"
          :rules="rules1"
          label-width="130px"
          class="edit-form-wrap"
        >
          <el-form-item label="配置开关" prop="status">
            <el-switch
              v-model="ruleForm1.status"
              inactive-color="#d4dbf5"
            ></el-switch>
            <span style="margin-left: 10px" v-if="ruleForm1.status">开启</span>
            <span style="margin-left: 10px" v-else>关闭</span>
          </el-form-item>
          <el-form-item label="appKey" prop="appKey" v-if="ruleForm1.status">
            <el-input
              v-model.trim="ruleForm1.appKey"
              placeholder="appKey"
              maxlength="30"
              clearable
            />
          </el-form-item>
          <el-form-item
            label="appSecret"
            prop="appSecret"
            v-if="ruleForm1.status"
          >
            <el-input
              v-model.trim="ruleForm1.appSecret"
              placeholder="请输入appSecre"
              maxlength="30"
              clearable
            />
          </el-form-item>
          <el-form-item
            label="销方税号"
            prop="salerTaxNum"
            v-if="ruleForm1.status"
          >
            <el-input
              v-model.trim="ruleForm1.salerTaxNum"
              placeholder="请输入销售方的纳税人识别号"
              maxlength="20"
              clearable
            />
          </el-form-item>
          <el-form-item
            label="销方电话"
            prop="salerTel"
            v-if="ruleForm1.status"
          >
            <el-input
              v-model.trim="ruleForm1.salerTel"
              placeholder="请输入销售方的电话"
              maxlength="20"
              clearable
            />
          </el-form-item>
          <el-form-item
            label="销方地址"
            prop="salerAddress"
            v-if="ruleForm1.status"
          >
            <el-input
              v-model.trim="ruleForm1.salerAddress"
              placeholder="请输入销售方的地址"
              maxlength="80"
              clearable
            />
          </el-form-item>
          <el-form-item
            label="销方开户行及账号"
            prop="salerAccount"
            v-if="ruleForm1.status"
            class="has-tips"
          >
            <el-input
              v-model.trim="ruleForm1.salerAccount"
              placeholder="请输入销售方的开户行和账号"
              maxlength="100"
              clearable
            />
            <div class="tip">
              <div class="icon"></div>
              示例：XXXXXX支行13000XXXXXXXXXXXX8
            </div>
          </el-form-item>
          <el-form-item label="收款人" prop="payee" v-if="ruleForm1.status">
            <el-input
              v-model.trim="ruleForm1.payee"
              placeholder="请输入收款人姓名，若填写信息会在发票中展示"
              maxlength="20"
              clearable
            />
          </el-form-item>
          <el-form-item label="复核人" prop="checker" v-if="ruleForm1.status">
            <el-input
              v-model.trim="ruleForm1.checker"
              placeholder="请输入复核人姓名，若填写信息会在发票中展示"
              maxlength="20"
              clearable
            />
          </el-form-item>
          <el-form-item label="开票员" prop="clerk" v-if="ruleForm1.status">
            <el-input
              v-model.trim="ruleForm1.clerk"
              placeholder="请输入开票员的姓名，若填写信息会在发票中展示"
              maxlength="20"
              clearable
            />
          </el-form-item>
          <el-form-item
            label="电费税率"
            prop="powerTaxRate"
            v-if="ruleForm1.status"
          >
            <el-input
              v-model.trim="ruleForm1.powerTaxRate"
              placeholder="请设置电费的税率，申请开票金额为含税价"
              maxlength="5"
              clearable
            >
              <template #append>%</template>
            </el-input>
          </el-form-item>
          <el-form-item
            label="服务费税率"
            prop="serviceTaxRate"
            v-if="ruleForm1.status"
          >
            <el-input
              v-model.trim="ruleForm1.serviceTaxRate"
              placeholder="请设置服务费的税率，申请开票金额为含税价"
              maxlength="5"
              clearable
            >
              <template #append>%</template>
            </el-input>
          </el-form-item>
          <el-form-item
            label="超时占用费税率"
            prop="occupyTaxRate"
            v-if="ruleForm1.status"
          >
            <el-input
              v-model.trim="ruleForm1.occupyTaxRate"
              placeholder="请设置超时占用费的税率，申请开票金额为含税价"
              maxlength="5"
              clearable
            >
              <template #append>%</template>
            </el-input>
          </el-form-item>
        </el-form>
        <div class="edit-btn-wrap">
          <el-button type="primary" plain @click="back">取消</el-button>
          <el-button type="primary" v-btnAntiShake="onSubmit1">保存</el-button>
        </div>
      </el-tab-pane>
      <el-tab-pane
        v-if="bntAuth('videosurveillanceList')"
        label="监控配置"
        name="monitor"
      >
        <Head
          title="平台默认对接监控平台为“萤石”，以下配置信息可从萤石开放平台“账号中心->应用信息”页面获取"
        >
          <template #content>
            <el-form
              ref="ruleFormMonitorRef"
              :model="ruleForm2"
              :rules="rules2"
              label-width="130px"
              class="edit-form-wrap"
            >
              <el-form-item label="AppKey" prop="appKey">
                <el-input
                  v-model.trim="ruleForm2.appKey"
                  placeholder="请输入AppKey"
                  maxlength="40"
                  clearable
                />
              </el-form-item>
              <el-form-item label="AppSecret" prop="appSecret">
                <el-input
                  v-model.trim="ruleForm2.appSecret"
                  placeholder="请输入AppSecret"
                  maxlength="40"
                  clearable
                />
              </el-form-item>
            </el-form>
          </template>
        </Head>
        <div class="edit-btn-wrap">
          <el-button type="primary" plain @click="back">取消</el-button>
          <el-button type="primary" v-btnAntiShake="onSubmit2">保存</el-button>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus';
import Head from '@/components/head/index.vue';
import {
  queryChannelConfigDetail,
  addConfig,
  getPaymentConfig,
  getNNConfigByChannelId,
  changeNNConfig,
} from '@/service/apis/channel';
import { queryMonitorKey, setMonitorKey } from '@/service/apis/monitor';
import { IS_INTEGER, IS_3TO1 } from '@/utils/verify';
import { bntAuth } from '@/utils/utils';

const route = useRoute();
const router = useRouter();
const {
  query: { id },
} = route;
console.log(import.meta.env.VITE_ENV_TYPE);

const lznt = import.meta.env.VITE_ENV_TYPE === 'lznt';
const ruleFormSmsRef = ref<FormInstance>();
const ruleFormWxRef = ref<FormInstance>();
const ruleFormYlRef = ref<FormInstance>();
const ruleFormIotRef = ref<FormInstance>();
const ruleFormInvoiceRef = ref<FormInstance>();
const ruleFormMonitorRef = ref<FormInstance>();

getPaymentConfig({}).then((res: any) => {
  if (res) {
    ruleForm.platfromType = res === '0' ? 1 : 2;
    console.log(res);
  }

  // if (ruleForm.platfromType !== 1) {
  //   ruleForm.type.push('银联支付配置');
  // }
});

// const getTypes = () => {
//   return ruleForm.platfromType === 1 ? CHANNEL_CONFIG : CHANNEL_CONFIG1;
// };

// const getTypes1 = () => {
//   return ruleForm.platfromType === 1 ? CHANNEL_CONFIG2 : CHANNEL_CONFIG3;
// };

const data = reactive({
  activeTab: 'sms',
  ruleForm: {} as any,
});

const ruleForm = reactive({
  platfromType: 1,
  channelId: id,
  id: '',
  // type: ['短信通道配置', '微信支付配置'],
  type: false,
  configType: '0',
  accesskeyId: '',
  accesskeySecret: '',
  signName: '',
  wechatAccount: '',
  appId: '',
  appSecret: '',
  merchantSecret: '',
  aplvSecret: '',
  serialNum: '',
  enterpriseKey: '',
  messageKey: '',
  initial: '',
  signatureKey: '',
  trafficUrl: '',
  umsAppId: '',
  umsAppKey: '',
  umsMerchant: '',
  umsTerminal: '',
  umsCommunicate: '',
  umsSystem: '',
  appkey: '',
});

const rules = reactive<FormRules>({
  accesskeyId: [
    { required: true, message: '请输入accessKeyId', trigger: 'blur' },
  ],
  accesskeySecret: [
    { required: true, message: '请输入accesskeySecret', trigger: 'blur' },
  ],
  signName: [{ required: true, message: '请输入短信签名', trigger: 'blur' }],
  wechatAccount: [
    { required: true, message: '请输入微信商户号', trigger: 'blur' },
  ],
  appId: [{ required: true, message: '请输入AppID', trigger: 'blur' }],
  appSecret: [{ required: true, message: '请输入AppSecret', trigger: 'blur' }],
  merchantSecret: [
    { required: true, message: '请输入商户私钥', trigger: 'blur' },
  ],
  aplvSecret: [{ required: true, message: '请输入APIv3秘钥', trigger: 'blur' }],
  serialNum: [{ required: true, message: '请输入证书序列号', trigger: 'blur' }],
  enterpriseKey: [
    { required: true, message: '请输入企业密钥', trigger: 'blur' },
  ],
  messageKey: [{ required: true, message: '请输入消息密钥', trigger: 'blur' }],
  initial: [{ required: true, message: '请输入初始化向量', trigger: 'blur' }],
  signatureKey: [
    { required: true, message: '请输入签名密钥', trigger: 'blur' },
  ],
  trafficUrl: [
    { required: true, message: '请输入互联互通方接口url', trigger: 'blur' },
  ],
  umsAppId: [{ required: true, message: '请输入银联AppId', trigger: 'blur' }],
  umsAppKey: [{ required: true, message: '请输入银联AppKey', trigger: 'blur' }],
  umsMerchant: [
    { required: true, message: '请输入银联商户号Mid', trigger: 'blur' },
  ],
  umsTerminal: [
    { required: true, message: '请输入银联终端号tid', trigger: 'blur' },
  ],
  umsCommunicate: [
    { required: true, message: '请输入银联通讯密钥', trigger: 'blur' },
  ],
  umsSystem: [
    { required: true, message: '请输入银联系统编号', trigger: 'blur' },
  ],
});

const ruleForm1: any = reactive({
  channelId: id,
  status: false,
  appKey: '',
  appSecret: '',
  salerTaxNum: '',
  salerTel: '',
  salerAddress: '',
  salerAccount: '',
  checker: '',
  payee: '',
  clerk: '',
  powerTaxRate: '',
  serviceTaxRate: '',
  occupyTaxRate: '',
});

const rules1 = reactive<FormRules>({
  status: [{ required: true, message: '请选择配置开关', trigger: 'blur' }],
  appKey: [{ required: true, message: '请输入appKey', trigger: 'blur' }],
  appSecret: [{ required: true, message: '请输入AppSecret', trigger: 'blur' }],
  salerTaxNum: [
    { required: true, message: '请输入销售方的纳税人识别号', trigger: 'blur' },
  ],
  salerTel: [
    { required: true, message: '请输入销售方的电话', trigger: 'blur' },
    {
      pattern: IS_INTEGER,
      message: '请输入数字',
      trigger: 'blur',
    },
  ],
  salerAddress: [
    { required: true, message: '请输入销售方的地址', trigger: 'blur' },
  ],
  salerAccount: [
    { required: false, message: '请输入销售方的开户行和账号', trigger: 'blur' },
  ],
  checker: [{ required: false, message: '请输入复核人姓名', trigger: 'blur' }],
  payee: [{ required: false, message: '请输入收款人姓名', trigger: 'blur' }],
  clerk: [{ required: true, message: '请输入开票员的姓名', trigger: 'blur' }],
  powerTaxRate: [
    { required: true, message: '请设置电费的税率', trigger: 'blur' },
    {
      pattern: IS_3TO1,
      message: '税率范围（0.0~999.9）',
      trigger: 'blur',
    },
  ],
  serviceTaxRate: [
    { required: true, message: '请设置服务费的税率', trigger: 'blur' },
    {
      pattern: IS_3TO1,
      message: '税率范围（0.0~999.9）',
      trigger: 'blur',
    },
  ],
  occupyTaxRate: [
    { required: true, message: '请设置超时占用费的税率', trigger: 'blur' },
    {
      pattern: IS_3TO1,
      message: '税率范围（0.0~999.9）',
      trigger: 'blur',
    },
  ],
});

const ruleForm2: any = reactive({
  id: '',
  channelId: id,
  appKey: '',
  appSecret: '',
});

const rules2 = reactive<FormRules>({
  appKey: [{ required: true, message: '请输入AppKey', trigger: 'blur' }],
  appSecret: [{ required: true, message: '请输入AppSecret', trigger: 'blur' }],
});

const back = () => {
  router.go(-1);
};

// 获取配置信息
queryChannelConfigDetail({ channelId: id }).then((res: any) => {
  console.log('res---', res);
  if (res) {
    ruleForm.configType = res.configType || '0';
    if (res.configType === '1') {
      ruleForm.type = true;
      //   ruleForm.type.push('互联互通配置');
    } else {
      ruleForm.type = false;
    }
    ruleForm.id = res.id;
    ruleForm.accesskeyId = res.accesskeyId;
    ruleForm.accesskeySecret = res.accesskeySecret;
    ruleForm.signName = res.signName;
    ruleForm.wechatAccount = res.wechatAccount;
    ruleForm.appId = res.appId;
    ruleForm.appSecret = res.appSecret;
    ruleForm.merchantSecret = res.merchantSecret;
    ruleForm.aplvSecret = res.aplvSecret;
    ruleForm.serialNum = res.serialNum;
    ruleForm.enterpriseKey = res.enterpriseKey;
    ruleForm.messageKey = res.messageKey;
    ruleForm.initial = res.initial;
    ruleForm.signatureKey = res.signatureKey;
    ruleForm.trafficUrl = res.trafficUrl;

    if (ruleForm.platfromType !== 1) {
      ruleForm.umsAppId = res.unionPayDTO ? res.unionPayDTO.appId : null;
      ruleForm.umsAppKey = res.unionPayDTO ? res.unionPayDTO.appKey : null;
      ruleForm.umsMerchant = res.unionPayDTO
        ? res.unionPayDTO.umsMerchant
        : null;
      ruleForm.umsTerminal = res.unionPayDTO
        ? res.unionPayDTO.umsTerminal
        : null;
      ruleForm.umsCommunicate = res.unionPayDTO
        ? res.unionPayDTO.umsCommunicate
        : null;
      ruleForm.umsSystem = res.unionPayDTO ? res.unionPayDTO.umsSystem : null;
    }

    for (const key in data.ruleForm) {
      if (String(res[key])) {
        data.ruleForm[key] = res[key];
      }
    }
    if (data.ruleForm.platfromType !== 1) {
      data.ruleForm.umsAppId = res.unionPayDTO ? res.unionPayDTO.appId : null;
      data.ruleForm.umsAppKey = res.unionPayDTO ? res.unionPayDTO.appKey : null;
      data.ruleForm.umsMerchant = res.unionPayDTO
        ? res.unionPayDTO.umsMerchant
        : null;
      data.ruleForm.umsTerminal = res.unionPayDTO
        ? res.unionPayDTO.umsTerminal
        : null;
      data.ruleForm.umsCommunicate = res.unionPayDTO
        ? res.unionPayDTO.umsCommunicate
        : null;
      data.ruleForm.umsSystem = res.unionPayDTO
        ? res.unionPayDTO.umsSystem
        : null;
    }
  }
});

// 获取发票配置信息
const getConfigByChannelId = () => {
  getNNConfigByChannelId(id).then((res: any) => {
    console.log('res---', res);
    if (res) {
      for (const key in ruleForm1) {
        if (String(res[key])) {
          ruleForm1[key] = res[key];
        }
      }
      ruleForm1.status = res.status == 0 ? false : true;
    }
  });
};

// 获取监控配置信息
const getMonitorConfig = () => {
  queryMonitorKey(id).then((res: any) => {
    ruleForm2.id = res?.id || '';
    ruleForm2.appSecret = res?.appSecret || '';
    ruleForm2.appKey = res?.appKey || '';
  });
};

const handleClickTab = (e: any) => {
  console.log(e.props.name);
  data.activeTab = e.props.name;
  if (e.props.name === '2') {
    getConfigByChannelId();
  } else if (e.props.name === 'monitor') {
    getMonitorConfig();
  } else {
    // getSettingsSms();
  }
};
const emit = defineEmits(['clear-cache']);
const onSubmit = async () => {
  console.log(ruleForm.platfromType);
  let form: any = {};
  if (data.activeTab == 'sms') {
    if (!ruleFormSmsRef.value) return;
    try {
      await Promise.all([ruleFormSmsRef.value.validate()]);
    } catch (fields: any) {
      for (const key in ruleForm) {
        if (fields[key]) {
          ruleFormSmsRef.value.scrollToField(key);
          break;
        }
      }
      return;
    }
    form.id = ruleForm.id;
    form.channelId = ruleForm.channelId;
    form.configType = ruleForm.configType;
    form.accesskeyId = ruleForm.accesskeyId;
    form.accesskeySecret = ruleForm.accesskeySecret;
    form.signName = ruleForm.signName;
    console.log('form.signName====', form.signName);
  }

  if (data.activeTab == 'wx') {
    if (
      !ruleFormWxRef.value ||
      (ruleForm.platfromType !== 1 && !ruleFormYlRef.value)
    )
      return;
    try {
      if (ruleForm.platfromType !== 1) {
        await Promise.all([
          ruleFormWxRef.value.validate(),
          ruleFormYlRef.value?.validate(),
        ]);
      } else {
        await Promise.all([ruleFormWxRef.value.validate()]);
      }
    } catch (fields: any) {
      for (const key in ruleForm) {
        if (fields[key]) {
          ruleFormWxRef.value.scrollToField(key);
          if (ruleForm.platfromType !== 1) {
            ruleFormYlRef.value?.scrollToField(key);
          }
          break;
        }
      }
      return;
    }
    form.id = ruleForm.id;
    form.channelId = ruleForm.channelId;
    form.configType = ruleForm.configType;
    form.appId = ruleForm.appId;
    form.appSecret = ruleForm.appSecret;
    form.wechatAccount = ruleForm.wechatAccount;
    form.aplvSecret = ruleForm.aplvSecret;
    form.merchantSecret = ruleForm.merchantSecret;
    form.serialNum = ruleForm.serialNum;
    console.log(111111111, ruleForm.aplvSecret);

    if (ruleForm.platfromType !== 1) {
      form.unionPayDTO = {
        appId: ruleForm.umsAppId,
        appKey: ruleForm.umsAppKey,
        umsMerchant: ruleForm.umsMerchant,
        umsTerminal: ruleForm.umsTerminal,
        umsCommunicate: ruleForm.umsCommunicate,
        umsSystem: ruleForm.umsSystem,
        wxId: ruleForm.appId,
        wxSecret: ruleForm.appSecret,
      };
    }
  }

  if (data.activeTab == '1') {
    if (!ruleFormIotRef.value) return;
    try {
      await Promise.all([ruleFormIotRef.value.validate()]);
    } catch (fields: any) {
      for (const key in ruleForm) {
        if (fields[key]) {
          ruleFormIotRef.value.scrollToField(key);
          break;
        }
      }
      return;
    }
    form.id = ruleForm.id;
    form.channelId = ruleForm.channelId;
    form.configType = ruleForm.type ? '1' : '0';
    form.enterpriseKey = ruleForm.enterpriseKey;
    form.messageKey = ruleForm.messageKey;
    form.initial = ruleForm.initial;
    form.signatureKey = ruleForm.signatureKey;
    form.trafficUrl = ruleForm.trafficUrl;
    console.log('type====', ruleForm.type);
  }

  form = Object.assign(form, data.ruleForm);
  console.log('ruleForm-----', form);
  addConfig(form).then((res: any) => {
    emit('clear-cache');
    back();
  });
};
const onSubmit1 = async () => {
  if (!ruleFormInvoiceRef.value) return;
  try {
    await Promise.all([ruleFormInvoiceRef.value.validate()]);
  } catch (fields: any) {
    for (const key in ruleForm1) {
      if (fields[key]) {
        ruleFormInvoiceRef.value.scrollToField(key);
        break;
      }
    }
    return;
  }
  let form: any = {};

  if (!ruleForm1.status) {
    form.status = 0;
  } else {
    form = Object.assign(form, ruleForm1);
    form.status = 1;
  }
  form.id = ruleForm.id;
  form.channelId = ruleForm.channelId;
  console.log(form);

  changeNNConfig(form).then((res: any) => {
    emit('clear-cache');
    back();
  });
};

const onSubmit2 = async () => {
  if (!ruleFormMonitorRef.value) return;
  try {
    await Promise.all([ruleFormMonitorRef.value.validate()]);
  } catch (fields: any) {
    for (const key in ruleForm2) {
      if (fields[key]) {
        ruleFormMonitorRef.value.scrollToField(key);
        break;
      }
    }
    return;
  }
  const form = Object.assign({}, ruleForm2);
  if (!form.id) {
    delete form.id;
  }

  setMonitorKey(form).then(() => {
    emit('clear-cache');
    back();
  });
};
</script>